package com.tboss.template.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.tboss.global.services.mail.VelocityUser;
import com.tboss.template.system.dto.UserInfoDto;
import com.tboss.template.system.service.UserInfoService;

@Controller
@RequestMapping(value = "/template/system/userInfo")
public class UserInfoController {
	
	@Autowired private UserInfoService usrInfoService;
	
	@RequestMapping(value = {"/_list{reqPage}", "/_list/{reqPage}", "/_listConts/{reqPage}"}, 
			method = {RequestMethod.POST, RequestMethod.GET})//
	public String usrList(@PathVariable("reqPage") Integer reqPage, @ModelAttribute UserInfoDto dto,
			Model model, HttpServletRequest req){
		
		String rtnUrl = req.getServletPath().substring(1);	
		if(reqPage != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"));
		
		this.usrInfoService.setModelUsrInfoList(reqPage, model, dto);

		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regForm"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String usrRegForm(Model model, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);	
		
		this.usrInfoService.usrRegForm(model);
		
		return rtnUrl+"Ajax";
	}

	@RequestMapping(value = {"/_regFormSave"}, method = {RequestMethod.POST})//
	public Model usrRegFormSave(@ModelAttribute UserInfoDto dto, Model model, HttpServletRequest req){
		this.usrInfoService.addUsrInfo(dto);
		//this.mapUsrInfoGrpService.addMapUsrInfoGrp(dto);
		
		return model;
	}
	
	@RequestMapping(value = {"/_regFormRevise/{usrId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String usrRegFormRevise(Model model, @PathVariable("usrId") String usrId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(usrId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("Revise/"+usrId));
		
		this.usrInfoService.usrRegForm(model, usrId, req);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regFormReviseSave"}, method = {RequestMethod.POST})//
	public Model usrRegFormReviseSave(@ModelAttribute UserInfoDto dto, Model model, HttpServletRequest req){
		this.usrInfoService.updateUsrInfo(dto, req);
		//this.mapUsrInfoGrpService.addMapUsrInfoGrp(dto);
		
		return model;
	}
	
	@RequestMapping(value = {"/_detail/{usrId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String usrInfoDetail(Model model, @PathVariable("usrId") String usrId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(usrId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+usrId));
		
		this.usrInfoService.usrRegForm(model, usrId, req);
		
		return rtnUrl+"Ajax";
	}
	
	//{address:.+}
	@RequestMapping(value = {"/_sendMailForm/{usrId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String usrInfoSendMailForm(Model model, @PathVariable("usrId") String usrId, HttpServletRequest req){		
		String rtnUrl = req.getServletPath().substring(1);
		if(usrId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+usrId));
		
		this.usrInfoService.usrInfoSendMailForm(model, usrId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_sendMailFormSend"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public Model usrInfoSendMailFormSend(Model model
			, @ModelAttribute VelocityUser dto
			, @RequestParam("file") List<MultipartFile> files
			, HttpServletRequest req){

		this.usrInfoService.usrInfoSendMailFormSend(model, dto, files);
		
		return model;
	}
	
}
